﻿
var _table = null;

layui.use(['form', 'layer'], function () {
    var form = layui.form,
        layer = layui.layer;

    $(function () {
        _table = $('#table');
        loadList();
    });
});

//查询
function search() {
    _table.bootstrapTable('destroy');
    loadList();
}

//初始化表格
function loadList() {
    app.list('MenuManage.ashx', {
        ActionType: 'Menu',
        MenuName: $('#tbKey').val()
    }, function (data) {
        _table.bootstrapTable({
            data: data,
            idField: 'GUID',
            singleSelect: true,
            dataType: 'jsonp',

            // bootstrap-table-treegrid.js 插件配置 -- start
            //在哪一列展开树形

            treeShowField: 'MenuName',
            //指定父id列
            parentIdField: 'ParentId',
            onResetView: function (data) {
                //console.log('load');
                _table.treegrid({
                    initialState: 'collapsed', // 所有节点都折叠
                    //initialState: 'expanded',// 所有节点都展开，默认展开
                    treeColumn: 1,
                    // expanderExpandedClass: 'glyphicon glyphicon-minus',  //图标样式
                    // expanderCollapsedClass: 'glyphicon glyphicon-plus',
                    onChange: function () {
                        _table.bootstrapTable('resetWidth');
                    }
                });

                //只展开树形的第一级节点
                //$table.treegrid('getRootNodes').treegrid('expand');
            }
            // bootstrap-table-treetreegrid.js 插件配置 -- end
        });
    });
}

//格式化操作按钮
function formatterOperationMenu(value, row, index) {
    var acthtml = '';
    var opHtml = $('#bst_tableEvent').html();
    if (opHtml) {
        var eventJson = $.parseJSON(opHtml);
        $.each(eventJson, function (idx, item) {
            if (item.MenuName == '分配按钮') {
                if (row.NavigateUrl) {
                    acthtml += '<a class="btn-' + item.MenuEvent + '" onclick="' + item.MenuEvent + '(\'' + row["GUID"] + '\',\'' + row["MenuName"] + '\')">' + item.MenuName + '</a>';
                }
            } else {
                acthtml += '<a class="btn-' + item.MenuEvent + '" onclick="' + item.MenuEvent + '(\'' + row["GUID"] + '\')">' + item.MenuName + '</a>';
            }
        });
    }
    return acthtml;
}

//查看所有图标
function viewIconfont() {
    window.open('/resources/font/demo_index.html');
}

//新增
function add() {
    var pid = '';
    var selRows = _table.bootstrapTable("getSelections");
    if (selRows.length > 1) {
        layer.msg("请选择一行");
        return;
    }
    if (selRows.length == 1) {
        pid = selRows[0].GUID;
    }

    layer.open({
        title: "菜单配置",
        type: 2,
        area: ["100%", "100%"],
        scrollbar: false,
        content: "MenuSet.aspx?ParentId=" + pid
    });
}

//编辑
function edit(id) {
    layer.open({
        title: "菜单配置",
        type: 2,
        area: ["100%", "100%"],
        scrollbar: false,
        content: "MenuSet.aspx?GUID=" + id
    });
}

//删除
function del(id) {
    layer.confirm('确定删除?', {
        icon: 3,
        title: '提示'
    }, function (index) {
        app.del('MenuManage.ashx', {
            ActionType: 'Menu',
            GUID: id
        }, function (data) {
            layer.msg(data.msg);
            if (data.success) {
                search();
            }
        });
        layer.close(index);
    });
}

//分配按钮
function allotButton(id, menuName) {
    layer.open({
        title: "分配按钮-" + menuName,
        type: 2,
        area: ["100%", "100%"],
        scrollbar: false,
        content: "MenuButtonSet.aspx?pid=" + id
    });
}
